package com.hzt.agprosaleplatform.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hzt.agprosaleplatform.common.CustomResponse;
import com.hzt.agprosaleplatform.entity.Article;
import com.hzt.agprosaleplatform.entity.Expert;
import com.hzt.agprosaleplatform.entity.ExpertQuestion;
import com.hzt.agprosaleplatform.entity.ShortQuestionDTO;
import com.hzt.agprosaleplatform.service.ExpertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/expert")
@CrossOrigin(origins = "http://localhost:5173")
public class ExpertController {
    @Autowired
    ExpertService expertService;

    /**
     * 首页获取专家小卡片信息
     * @return 3条专家信息
     */
    @GetMapping("/homePageExpert")
    public CustomResponse selectThreeExpert(){
        try {
            List<Expert> expertsList = expertService.RecommendedExperts();
            return new CustomResponse(200, "获取推荐专家成功", expertsList);
        } catch (Exception e) {
            return new CustomResponse(500, "获取推荐专家失败", null);
        }
    }

    @GetMapping("/expertInfo/{uid}")
    public CustomResponse getExpertInfo(@PathVariable String uid) {
        try{
            return new CustomResponse(200,"获取专家信息成功",expertService.selectExpertById(uid));
        }catch (Exception e){
            return new CustomResponse(500, "获取推荐专家失败", null);
        }
    }


    /**
     * EID下已回复的专家问答帖子  status = 1
     * @param uid
     * @return
     */
    @GetMapping("/expertQuestion/{uid}")
    public CustomResponse getExpertQuestion(@PathVariable String uid) {
        try{
            List<ShortQuestionDTO> questionList = expertService.shortQuestionList(uid);
            return new CustomResponse(200,"OK",questionList);
        }catch (Exception e){
            return  new CustomResponse(500,"ERROR",null);
        }
    }

    /**
     * 专家-用户查询所有问答小卡片
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/ListAllQuestion")
    public CustomResponse pageForAllQuestion(
            @RequestParam(defaultValue = "1")Integer pageNum,
            @RequestParam(defaultValue = "12")Integer pageSize
    ){
        IPage<ExpertQuestion> expertQuestionIPage = expertService.ListAllQuestion(pageNum,pageSize);
        return new CustomResponse(200,"OK",expertQuestionIPage);
    }

    /**
     * 获取文章推荐小卡片
     * @return
     */
    @GetMapping("/articleCard")
    public CustomResponse getArticleCard(){
        try{
            List<Article> articles = expertService.getArticleCard();
            return new CustomResponse(200,"OK",articles);
        }catch (Exception e){
            return  new CustomResponse(500,"服务器出错了!",null);
        }
    }

    /**
     * 发布专家问答帖子
     * @param expertQuestion
     * @return
     */
    @PostMapping("/QsCreation")
    public CustomResponse addNewQuestion(@RequestBody ExpertQuestion expertQuestion){
        try {
             ExpertQuestion qs = expertService.addNewQuestion(expertQuestion);
             if(qs != null){
                 return new CustomResponse(200,"请求提问成功",qs);
             }else {
                 return new CustomResponse(400,"提问失败",null);
             }
        }catch (Exception e){
            return new CustomResponse(500,"系统错误",null);
        }
    }


    /**
     * 根据Qid获取问题
     * @param qid
     * @return
     */
    @GetMapping("/Question")
    public CustomResponse ExpertQuestionByGid(@RequestParam int qid){
        try{
            return new CustomResponse(200,"OK",expertService.selectQuestionByQid(qid));
        }catch (Exception e){
            return new CustomResponse(500,"ERROR",null);
        }
    }

    /**
     * 专家回复帖子
     * @param qid
     * @param answer
     * @return
     */
    @PostMapping("/answer")
    public CustomResponse answerQuestion(@RequestParam Integer qid,@RequestParam String answer){
        try{
            ExpertQuestion result = expertService.answerQuestion(qid,answer);
            if(result != null){
                return new CustomResponse(200,"回答成功",result);
            }
            return new CustomResponse(400,"连接不稳定，回复失败",null);
        }catch (Exception e){
            return new CustomResponse(500,"服务器响应错误",null);
        }
    }

    @PostMapping("/moreAnswer")
    public CustomResponse moreAnswerToQuestion(
            @RequestParam String uid,
            @RequestParam Integer qid,
            @RequestParam String answer){
        try{
            String result = expertService.moreAnswer(uid,qid,answer);
            return new CustomResponse(200,"回复成功",result);
        }catch (Exception e){
            return new CustomResponse(500,"回复失败",null);
        }
    }

    @GetMapping("/getMoreAnswer")
    public CustomResponse getMoreAnswer(@RequestParam Integer qid){
        try{
            List<ExpertQuestion> questionList = expertService.getMoreAnswer(qid);
            return new CustomResponse(200,"OK",questionList);
        }catch (Exception e){
            return new CustomResponse(500,"ERROR",null);
        }
    }
    /**
     * 申请成为专家，插入信息但是审核状态任然为0
     * @param expert
     * @return
     */
    @PostMapping("/BeExpert")
    public CustomResponse BeExpert(@RequestBody Expert expert){
        try {
            expertService.BeExpert(expert);
            return new CustomResponse(200,"递交申请成功，等待审核通过",null);
        }catch (Exception e){
            return new CustomResponse(500,"服务出错了！",null);
        }
    }

    @PostMapping("/article")
    public CustomResponse postArticle(
            @RequestPart("article") Article article,
            @RequestPart("files") List<MultipartFile> files) {
        try {
            // 参数验证
            if (article.getTitle() == null || article.getTitle().trim().isEmpty()) {
                return new CustomResponse(400, "标题不能为空", null);
            }
            if (files == null || files.isEmpty()) {
                return new CustomResponse(400, "至少需要一张封面图片", null);
            }

            Article savedArticle = expertService.postArticle(article, files);
            
            return new CustomResponse(200, "文章发布成功", savedArticle);
        } catch (Exception e) {
            e.printStackTrace();
            return new CustomResponse(500, "文章发布失败: " + e.getMessage(), null);
        }
    }

    @GetMapping("/articleDetail/{id}")
    public CustomResponse getArticle(@PathVariable Integer id) {
        try {
            Map<String, Object> articleData = expertService.getArticleWithImages(id);
            return new CustomResponse(200, "获取文章成功", articleData);
        } catch (Exception e) {
            return new CustomResponse(500, "获取文章失败: " + e.getMessage(), null);
        }
    }
}
